﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace umsoftU3.Common
{
    public class AppFunctions
    {
        /// <summary>
        /// 小写金额转换成大写金额
        /// </summary>
        /// <param name="vValue"></param>
        /// <returns></returns>
        public static string ToInWords(string vValue)
        {
            #region
            //金额转换程序
            string MoneyNum = "";//记录小写金额字符串[输入参数]
            string MoneyStr = "";//记录大写金额字符串[输出参数]
            string BNumStr = "零壹贰叁肆伍陆柒捌玖";//模
            string UnitStr = "分角圆拾佰仟万拾佰仟亿拾佰仟万";//模

            decimal dclValue = 0;

            if (decimal.TryParse(vValue, out dclValue))
            {
                //精度为分，去掉小数点
                MoneyNum = ((long)(dclValue * 100)).ToString();

                //将数字转为大写
                for (int intIndex = MoneyNum.Length - 1; intIndex >= 0; intIndex--)
                {
                    string strTemp = MoneyNum.Substring(intIndex, 1);
                    int intNum = int.Parse(strTemp);
                    MoneyStr = BNumStr.Substring(intNum, 1) + UnitStr.Substring(MoneyNum.Length - 1 - intIndex, 1) + MoneyStr;
                }
            }

            //二次处理大写金额字符串
            MoneyStr = MoneyStr + "整";
            while (MoneyStr.Contains("零分") || MoneyStr.Contains("零角") || MoneyStr.Contains("零佰") || MoneyStr.Contains("零仟")
                || MoneyStr.Contains("零万") || MoneyStr.Contains("零亿") || MoneyStr.Contains("零零") || MoneyStr.Contains("零圆")
                || MoneyStr.Contains("亿万") || MoneyStr.Contains("零整") || MoneyStr.Contains("分整"))
            {
                MoneyStr = MoneyStr.Replace("零分", "零");
                MoneyStr = MoneyStr.Replace("零角", "零");
                MoneyStr = MoneyStr.Replace("零拾", "零");
                MoneyStr = MoneyStr.Replace("零佰", "零");
                MoneyStr = MoneyStr.Replace("零仟", "零");
                MoneyStr = MoneyStr.Replace("零万", "万");
                MoneyStr = MoneyStr.Replace("零亿", "亿");
                MoneyStr = MoneyStr.Replace("亿万", "亿");
                MoneyStr = MoneyStr.Replace("零零", "零");
                MoneyStr = MoneyStr.Replace("零圆", "圆零");
                MoneyStr = MoneyStr.Replace("零整", "整");
                MoneyStr = MoneyStr.Replace("分整", "分");
            }
            if (MoneyStr == "整")
            {
                MoneyStr = "零元整";
            }
            return MoneyStr;
            #endregion
        }
    }
}
